<?php
class DeployTool
{
	public function main()
	{
		if (!isset($_SERVER['argv'][1]))
		{
			$this->help();
		}
		else
		{
			/**
			 * check the first argument: app option file
			 */
			$appOptionsFile = $_SERVER['argv'][1];
			$appOptions = include($appOptionsFile);
			if (!is_array($appOptions))
			{
				echo "Please return an array in file: $appOptionsFile";
				exit;
			}

			/**
			 * check the second argument: app bootstrap file
			 */
			if (!isset($_SERVER['argv'][2]))
			{
				if (!isset($appOptions['proj_dir']) || !isset($appOptions['app_name']))
				{
					echo "Please specify both 'proj_dir' and 'app_name' in the appOption array, or specify the app bootstrap file";
					exit;
				}
			}
			else
			{
				$appBootStrap = $_SERVER['argv'][2];
			}

			/**
			 * check the third argument: path_to_kiwi.php
			 */
			if (!isset($_SERVER['argv'][3]))
			{
				$kiwiHome = dirname(dirname(dirname(__FILE__)));
				$kiwiBootstrap = $kiwiHome . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR . 'kiwi.php';
				if (!file_exists($kiwiBootstrap))
				{
					echo "Please specify where is the 'kiwi.php' or leave the deploy script on the original place";
					exit;
				}
			}
			else
			{
				$kiwiBootstrap = $_SERVER['argv'][3];
			}

			/**
			 * load kiwi.php
			 */
			include($kiwiBootstrap);
			Kiwi::init($appOptions, true);
			$this->rebuildOpcodeCache();
		}
	}

	public function rebuildOpcodeCache()
	{
		$appOptions = Kiwi::$appOptions;
		$postData = isset($appOptions['opcode_cache_ext']) ? array('opcode_cache_ext' => $appOptions['opcode_cache_ext']) : array();
		/**
		 * recache app options
		 */
		$AppOptionCacheKey = KiwiSystemCache::getAppOptionCacheKey($appOptions['proj_dir'], $appOptions['app_name']);
		echo "Caching app option with key: $AppOptionCacheKey \n";
		$postData['cache_key'] = $AppOptionCacheKey;
		$postData['data_to_cache'] = serialize($appOptions);
		echo "[server response]" . Curl::post('http://deploytool.kiwiphp.localhost/opcode_cache_operator.php', $postData);

		/**
		 * recache class file mapping
		 */
		$classFileMappingCacheKey = KiwiSystemCache::getClassFileMappingCacheKey($appOptions['proj_dir'], $appOptions['app_name']);
		$classFileMapping = KiwiSystemCache::getClassFileMapping($classFileMappingCacheKey, $appOptions);
		echo "Caching class file mapping with key: $classFileMappingCacheKey \n";
		$postData['cache_key'] = $classFileMappingCacheKey;
		$postData['data_to_cache'] = serialize($classFileMapping);
		echo "[server response]" . Curl::post('http://deploytool.kiwiphp.localhost/opcode_cache_operator.php', $postData);

		/**
		 * recache project config
		 */
		$projectConfigCacheKey = KiwiSystemCache::getConfigCacheKey($appOptions['proj_dir']);
		$projectConfig = KiwiSystemCache::getConfig($projectConfigCacheKey, $appOptions['config_file']);
		echo "Caching project config with key: $projectConfigCacheKey \n";
		$postData['cache_key'] = $projectConfigCacheKey;
		$postData['data_to_cache'] = serialize($projectConfig);
		echo "[server response]" . Curl::post('http://deploytool.kiwiphp.localhost/opcode_cache_operator.php', $postData);

		/**
		 * recache app config
		 */
		$appConfigCacheKey = KiwiSystemCache::getConfigCacheKey($appOptions['proj_dir'], $appOptions['app_name']);
		$appConfig = KiwiSystemCache::getConfig($appConfigCacheKey, $appOptions['app_config_file'], $projectConfig);
		echo "Caching app config with key: $appConfigCacheKey \n";
		$postData['cache_key'] = $appConfigCacheKey;
		$postData['data_to_cache'] = serialize($appConfig);
		echo "[server response]" . Curl::post('http://deploytool.kiwiphp.localhost/opcode_cache_operator.php', $postData);
	}

	public function help()
	{
		echo "Usage: php " . basename(__FILE__) . " app_option_file [app_bootstrap_file path_to_kiwi.php]";
	}
}
class Curl
{
	public static function post($url, $data = null, $timeout = 5, $cookie = null)
	{
		$ch = curl_init();
		$options = array(
		CURLOPT_URL => $url,
		CURLOPT_POST => 1,
		CURLOPT_RETURNTRANSFER => 1,
		);

		if(!empty($data))
		{
			$options[CURLOPT_POSTFIELDS] = $data;
		}
		if(!empty($cookie))
		{
			$options[CURLOPT_COOKIE] = $cookie;
		}
		curl_setopt_array($ch, $options);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
		$data = curl_exec($ch);
		$errNo = curl_errno($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if($errNo != 0 || ($httpCode != 200 && $httpCode != 302)) {
			$errStr = curl_error($ch);
			error_log("Curl post $url failed with error:" . $errStr);
			return false;
		}
		curl_close($ch);
		return $data;
	}
}
$deployTool = new DeployTool();
$deployTool->main();
